import config from "./WebIMConfig";
import websdk from "easemob-websdk";
import store from '@/store'
import _ from "lodash";
import ring from "@/assets/ring.mp3";
import Vue from "vue";
// import wmplayer from '@/assets/js/wmplayer.js'
// let player = wmplayer('@/assets/ring.mp3')
const instance = new Audio();
instance.crossOrigin = "*";
instance.src = ring;
instance.load();

// 初始化IM SDK
let WebIM = {};
WebIM = window.WebIM = websdk;
WebIM.config = config;
WebIM.conn = new WebIM.connection({
  isReport: false,
  appKey: WebIM.config.appkey,
  isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
  https: WebIM.config.https,
  isAutoLogin: true,
  heartBeatWait: WebIM.config.heartBeatWait,
  autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
  autoReconnectInterval: WebIM.config.autoReconnectInterval,
  isStropheLog: WebIM.config.isStropheLog,
  delivery: WebIM.config.delivery,

  // 公有云 isHttpDNS 默认配置为true
  isHttpDNS: WebIM.config.isHttpDNS,
  // url: WebIM.config.url,
  // apiUrl: WebIM.config.apiUrl,

  // 私有云设置，详细文档：http://docs-im.easemob.com/im/web/other/privatedeploy

  // url: 'https://im-api-v2.easecdn.com/ws', // 设置为私有云的websocket server url
  // apiUrl: 'https://a1.easecdn.com', // 设置为私有云的rest server url

  // url: 'http://msync-api-a1-test.easemob.com:8081/ws', // 设置为私有云的websocket server url
  // url: window.location.protocol + '//msync-api-a1-test.easemob.com/ws', // 设置为私有云的websocket server url

  // url: 'https://im-api-v2-hsb.easemob.com/ws', // 设置为私有云的websocket server url
  // url: 'http://52.80.99.104:8081/ws', // 设置为私有云的websocket server url
  // apiUrl: 'http://a1-test.easemob.com:8089', // 设置为私有云的rest server url
  // apiUrl: window.location.protocol + '//a1-test.easemob.com', // 设置为私有云的rest server url
});
let myName = ''
// 注册监听回调
WebIM.conn.listen({
  onOpened: function (message) {
    // 连接成功回调
    // // 登录或注册成功后 跳转到好友页面
    // const username = Vue.$store.state.login.username || localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).userId;
    // Vue.$store.dispatch('getLoginUserInfo', { userId: username });
    // const path = location.pathname.indexOf('login') !== -1 || location.pathname.indexOf('register') !== -1 ? '/contact' : location.pathname;
    // const redirectUrl = `${path}?username=${username}`;
    // Vue.$router.push({ path: redirectUrl });
    // // Vue.$store.dispatch("getAllFriendsStatus");
    let userinfo = localStorage.getItem('EASEIM_loginUser')&&JSON.parse(localStorage.getItem('EASEIM_loginUser'))
    if(userinfo){
      myName = userinfo.user
    }
  },
  onClosed: function (message) {
    // Vue.$router.push({ path: "/login" });
  }, // 连接关闭回调
  onTextMessage: function (message) {
    console.log("onTextMessage", message);
    // 增加消息数
    if(message.from!=myName){
      store.commit('SET_UNREAD_MSG_NUM',store.state.im.unReadMsgNum+1)
      store.commit('SET_UNREAD_MAP',message)
      instance.play();
    }
  }, // 收到文本消息
  onCustomMessage: function (message) {
    console.log(">>>>>自定义消息", message);
    if(message.from!=myName){
      store.commit('SET_UNREAD_MSG_NUM',store.state.im.unReadMsgNum+1)
      store.commit('SET_UNREAD_MAP',message)
      instance.play();
    }
  }, // 收到自定义消息。

  onPresence: function (message) {
    console.log("onPresence", message);

    switch (message.type) {
      case "direct_joined": // 被拉进群--不需要同意
        Vue.$message.success(`${message.from}邀请您加入群：${message.gid}`);
        break;
      case "memberJoinPublicGroupSuccess": // 成员加入聊天室成功回调
        Vue.$message.success(`${message.from}已加入群组`);
        break;
      case "deleteGroupChat": // 解散群组
        Vue.$message.error(`${message.from}将群：${message.gid} 已解散`);
        break;
      case "removedFromGroup": // 移除
        Vue.$message.success("已被" + message.from + "移除群：" + message.gid);
        break;
      default:
        break;
    }
  }, // 处理“广播”或“发布-订阅”消息，如联系人订阅请求、处理群组、聊天室被踢解散等消息
  onRoster: function (message) {
    console.log("onRoster", message);
  }, // 处理好友申请
  onInviteMessage: function (message) {
    console.log("onInviteMessage", message);
  }, // 处理群组邀请
  onOnline: function () {
    console.log("onOnline 网络已连接");
  }, // 本机网络连接成功
  onOffline: function () {
    console.log("onOffline 网络已断开");
  }, // 本机网络掉线
});

// WebIM.WebRTC = window.webrtc; // 本项目引入 UMD 文件有问题，暂时这样引入
// WebIM.EMedia = window.emedia;
// // WebIM.AgoraRTC = AgoraRTC;
// WebIM.rtc = rtc;

export default WebIM;
